home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Tools / Text-Viewer / MSWordView / mswordview_src / gateway / file-upload.cgi < prev    next >
Encoding:
Text File  |  1999-11-06  |  14.6 KB  |  421 lines

  1. #!/usr/bin/perl
  2. #
  3. # File Upload Script, slightly modified by Caolan.McNamara@ul.ie to
  4. # serve as a msword to html gateway.
  5. # original notice follows
  6. #
  7. # modify lines 89 & 401 for your own configuration.
  8. #
  9. #
  10. #
  11. #
  12. # File Upload Script        Version 6.00
  13. # Created by Jeff Carnahan  jeffc@terminalp.com
  14. # Created on: 4/8/95        Last Modified on: 01/23/98 23:06
  15. # Scripts Archive:          http://www.terminalp.com/scripts/
  16. #
  17. # ---------------------------------------------------------------------
  18. #
  19. # Copyright (C) 1996 Jeffrey D. Carnahan
  20. #
  21. # This program is free software; you can redistribute it and/or modify
  22. # it under the terms of the GNU General Public License as published by
  23. # the Free Software Foundation; either version 2 of the License, or (at
  24. # your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful, but
  27. # WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. # General Public License for more details.
  30. #
  31. # A full copy of the GNU General Public License can be retrieved from
  32. # http://www.terminalp.com/scripts/license.shtml
  33. #
  34. # - Jeff Carnahan <jeffc@terminalp.com
  35. #
  36. # ---------------------------------------------------------------------
  37. # Program Specific Quickie Notes:
  38. #   * Make Sure The First Line Is Pointing To The Correct Location Of Perl 5.
  39. #   * Make Sure This Program is chmodded with the permissions '755'.
  40. #
  41. #  Version:  Time Stamp:        History:
  42. #  ____________________________________________________________________
  43. #
  44. #     1.00  04/08/96 00:00     The script was created.
  45. #     1.10  04/23/96 00:00     Added User and Group ID to allow file
  46. #                              changing by the actual user, also updated
  47. #                              a security hole which allowed any user with
  48. #                              the UID of 1376 to own the uploaded files.
  49. #                              Also Updated the INSTALL program and README
  50. #                              files.
  51. #     3.00  05/07/96 00:00     New release with group and user id fixes, it
  52. #                              updates a previously unreleased version (2.0)
  53. #     3.10  05/10/96 00:00     Stupid Typo in script fixed, it was
  54. #                              causing problems for some users.
  55. #     4.00  08/04/96 23:16     Security hole regarding '../' paths
  56. #                              fixed.  Thanks to: Rus Berrett.  Mime
  57. #                              type error fixed.  Thanks to: Bob Stewart.
  58. #     4.01  08/07/96 11:20     Typo fixed in &NoOpen.  Thanks to Marco
  59. #                              Dings.
  60. #     5.00  10/06/96 21:42     Fully rewrote script around CGI.pm library.
  61. #                              As soon as I get the time, I'll write more
  62. #                              features into it, but for now, this version
  63. #                              is stable (to the best of my knowledge).
  64. #     5.01  02/09/97 12:41     Fixed some typo's, and added support for
  65. #                              Netscape Communicator.
  66. #     5.02  05/07/97 15:37     Fixed a possible binary file uploading,
  67. #                              added easier support for NT, and fixed
  68. #                              documentation problems. Added the FAQ.
  69. #     5.03  06/19/97 17:30     Fixed a bug which resulted in all files
  70. #                              appearing to be less than one byte in
  71. #                              size, thus uploads weren't saved.
  72. #     6.00  01/23/98 23:06     Added multiple-file uploading.  You can
  73. #                              now upload more than one file at a time.
  74. #                              Also added $MAXIMUM_UPLOAD variable to
  75. #                              restrict file upload sizes.  Script 
  76. #                              completely re-written.  Removed buggy
  77. #                              NT support, and simplified variables.
  78. #                              CGI.pm is no longer bundled with this 
  79. #                              script.  If you need it, download it 
  80. #                              from:
  81. #       http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html
  82. #                             
  83. # ---------------------------------------------------------------------
  84. # Configurable Options Follow:
  85. #
  86.  
  87. BEGIN {
  88.  
  89.     $SAVE_DIRECTORY = "/home/sysadm/caolan/tmp/upload-mswordview";
  90.                               #
  91.                               # --> Variable:
  92.                               #         $SAVE_DIRECTORY
  93.                               # --> Function:
  94.                               #         Defines the path to the directory
  95.                               #         on the server that should be used
  96.                               #         as the folder to save files into.
  97.                               # --> Directory Permissions:
  98.                               #         a+rwx
  99.                               # --> Additional Notes:
  100.                               #         This path should not have a
  101.                               #         trailing forward slash.  Also
  102.                               #         remember that this is a path, not
  103.                               #         a URL.  Use something similar to:
  104.                               #
  105.                               #         /home/myself/www/uploads
  106.                               #
  107.  
  108.     $MAXIMUM_UPLOAD = 0;
  109.                               #
  110.                               # --> Variable:
  111.                               #         $MAXIMUM_UPLOAD
  112.                               # --> Function:
  113.                               #         Defines the number of bytes that
  114.                               #         can be uploaded.  Files that exceed
  115.                               #         this limit will not be saved on the
  116.                               #         server.
  117.                               # --> Additional Notes:
  118.                               #         Set this to zero in order to 
  119.                               #         disable size checking.
  120.                               #
  121.                               
  122.     $ALLOW_INDEX = 0;
  123.                               #
  124.                               # --> Variable:
  125.                               #         $ALLOW_INDEX
  126.                               # --> Function:
  127.                               #         If set to zero, files whose
  128.                               #         names begin with the word 
  129.                               #         index will not be saved.
  130.                               # 
  131.                               #         Set to one to allow files
  132.                               #         named index* to be uploaded.
  133.                               # --> Additional Notes:
  134.                               #
  135.                               
  136.     $SUCCESS_LOCATION = ""
  137.                               #
  138.                               # --> Variable:
  139.                               #         $SUCCESS_LOCATION
  140.                               # --> Function:
  141.                               #         Defines the URL that users
  142.                               #         should be redirected to if 
  143.                               #         the script works properly.  If
  144.                               #         this is left blank, a default
  145.                               #         page will be returned to the
  146.                               #         user.
  147.                               # --> Additional Notes:
  148.                               #         This is a COMPLETE URL, not
  149.                               #         a path.
  150. }
  151. #
  152. # End of Configurable Options.
  153. # ---------------------------------------------------------------------
  154. # ---------------------------------------------------------------------
  155. # -->           Do Not Change Anything Below This Line.           <-- #
  156. # ---------------------------------------------------------------------
  157. # ---------------------------------------------------------------------
  158.  
  159.     $| = 1;
  160.     chop $SAVE_DIRECTORY if ($SAVE_DIRECTORY =~ /\/$/);
  161.     use CGI qw(:standard);
  162.     $query = new CGI;
  163.  
  164.     if ( (!(-e $SAVE_DIRECTORY)) ||
  165.          (!(-W $SAVE_DIRECTORY)) ||
  166.          (!(-d $SAVE_DIRECTORY)) ) {
  167.         print header;
  168.         print <<__END_OF_HTML_CODE__;
  169.         
  170.         <HTML>
  171.         <HEAD>
  172.             <TITLE>Error: Bad Directory</TITLE>
  173.         </HEAD>
  174.         <BODY link="#CC0000" alink="#FF3300" vlink="#330099" text="#000000" bgcolor="#ffffff" background="../pics/weave.jpg">
  175.         <table border=0 width=590>  <!-- begin table-->
  176.         <tr>
  177.         <tr>
  178.         <td valign="top" width="110">
  179.         <img src="../pics/sidebar.gif" usemap="#sidebar.gif" border="0">
  180.         <br>
  181.         </td>
  182.         <td width="480">      <!-- total = 590 -->
  183.  
  184.         
  185.         <H1>Bad Directory</H1>
  186.         <P>
  187.         The directory you specified:
  188.         <BR>
  189.         <BLOCKQUOTE>
  190.             <TT>\$SAVE_DIRECTORY = "<B>$SAVE_DIRECTORY</B>";</TT>
  191.         </BLOCKQUOTE>
  192.         <BR>
  193.         is invalid.  This problem is caused by one of the three following reasons:
  194.         <OL>
  195.             <LI>The directory doesn't exist.  Make sure that this directory is a complete path name, not
  196.                 a URL or something similar.  It should look similar to <TT>/home/username/public_html/uploads</TT>
  197.             <P>
  198.             <LI>The directory isn't writable.  Make sure that this directory is writable by all users.  At
  199.                 your UNIX command prompt, type <TT>chmod 777 $SAVE_DIRECTORY</TT>
  200.             <P>
  201.             <LI>The directory you specified isn't really a directory.  Make sure that this is indeed a directory
  202.                 and not a file.
  203.         </OL>
  204.         <HR SIZE=1>
  205. <center>
  206. <A HREF="http://skynet.csn.ul.ie/"> <IMG Border="0" SRC="../pics/skynet-button.gif"></a>
  207. </center>
  208.         </td>
  209.         </tr>
  210.         </table>
  211.         </BODY>
  212.         </HTML>
  213.         
  214. __END_OF_HTML_CODE__
  215.         exit;
  216.     }
  217.     
  218.     foreach $key (sort {$a <=> $b} $query->param()) {
  219.         next if ($key =~ /^\s*$/);
  220.         next if ($query->param($key) =~ /^\s*$/);
  221.         next if ($key !~ /^file-to-upload-(\d+)$/);
  222.         $Number = $1;
  223.         
  224.         if ($query->param($key) =~ /([^\/\\]+)$/) {
  225.             $Filename = $1;
  226.             $Filename =~ s/^\.+//;
  227.             $File_Handle = $query->param($key);
  228.             
  229.             if (!$ALLOW_INDEX && $Filename =~ /^index/i) {
  230.                 print header;
  231.                 print <<__END_OF_HTML_CODE__;
  232.                 
  233.                 <HTML>
  234.                 <HEAD>
  235.                     <TITLE>Error: Filename Problem</TITLE>
  236.                 </HEAD>
  237.                 <BODY link="#CC0000" alink="#FF3300" vlink="#330099" text="#000000" bgcolor="#ffffff" background="../pics/weave.jpg">
  238.                 <table border=0 width=590>  <!-- begin table-->
  239.                 <tr>
  240.                 <tr>
  241.                 <td valign="top" width="110">
  242.                 <img src="../pics/sidebar.gif" usemap="#sidebar.gif" border="0">
  243.                 <br>
  244.                 </td>
  245.                 <td width="480">      <!-- total = 590 -->
  246.  
  247.                 <H1>Filename Problem</H1>
  248.                 <P>
  249.                 You attempted to upload a file that isn't properly formatted.  The system administrator
  250.                 has decided that you can't upload files that begin with the word '<B>index</B>'. Please
  251.                 rename the file on your computer, and try uploading it again.
  252.                 <P>
  253.                 <HR SIZE=1>
  254. <center>
  255. <A HREF="http://skynet.csn.ul.ie/"> <IMG Border="0" SRC="../pics/skynet-button.gif"></a>
  256. </center>
  257.  
  258.         </td>
  259.         </tr>
  260.         </table>
  261.                 </BODY>
  262.                 </HTML>
  263.     
  264. __END_OF_HTML_CODE__
  265.                 exit;
  266.             }
  267.         } else {
  268.             $FILENAME_IN_QUESTION = $query->param($key);
  269.             
  270.             print header;
  271.             print <<__END_OF_HTML_CODE__;
  272.             
  273.             <HTML>
  274.             <HEAD>
  275.                 <TITLE>Error: Filename Problem</TITLE>
  276.             </HEAD>
  277.             <BODY link="#CC0000" alink="#FF3300" vlink="#330099" text="#000000" bgcolor="#ffffff" background="../pics/weave.jpg">
  278.             <table border=0 width=590>  <!-- begin table-->
  279.             <tr>
  280.             <tr>
  281.             <td valign="top" width="110">
  282.             <img src="../pics/sidebar.gif" usemap="#sidebar.gif" border="0">
  283.             <br>
  284.             </td>
  285.             <td width="480">      <!-- total = 590 -->
  286.  
  287.  
  288.             <H1>Filename Problem</H1>
  289.             <P>
  290.             You attempted to upload a file that isn't properly formatted.  The file in question 
  291.             is <TT><B>$FILENAME_IN_QUESTION</B></TT>  Please rename the file on your computer, and
  292.             attempt to upload it again.  Files may not have forward or backward slashes in their 
  293.             names.  Also, they may not be prefixed with one (or more) periods.
  294.             <P>
  295.             <HR SIZE=1>
  296. <center>
  297. <A HREF="http://skynet.csn.ul.ie/"> <IMG Border="0" SRC="../pics/skynet-button.gif"></a>
  298. </center>
  299.  
  300.         </td>
  301.         </tr>
  302.         </table>
  303.             </BODY>
  304.             </HTML>
  305.  
  306. __END_OF_HTML_CODE__
  307.             exit;
  308.         }
  309.         
  310.         if (!open(OUTFILE, ">$SAVE_DIRECTORY\/$Filename")) {
  311.             print "Content-type: text/plain\n\n";
  312.             print "-------------------------\n";
  313.             print "Error:\n";
  314.             print "-------------------------\n";
  315.             print "File: $SAVE_DIRECTORY\/$Filename\n";
  316.             print "-------------------------\n";
  317.             print "There was an error opening the Output File\n";
  318.             print "for Writing.\n\n";
  319.             print "Make sure that the directory:\n";
  320.             print "$SAVE_DIRECTORY\n";
  321.             print "has been chmodded with the permissions '777'.\n\n";
  322.             print "Also, make sure that if your attempting\n";
  323.             print "to overwrite an existing file, that the\n";
  324.             print "existing file is chmodded '666' or better.\n\n";
  325.             print "The Error message below should help you diagnose\n";
  326.             print "the problem.\n\n";
  327.             print "Error: $!\n";
  328.             exit;
  329.         }
  330.  
  331.         undef $BytesRead;
  332.         undef $Buffer;
  333.         
  334.         while ($Bytes = read($File_Handle,$Buffer,1024)) {
  335.             $BytesRead += $Bytes;
  336.             print OUTFILE $Buffer;
  337.         }
  338.         
  339.         push(@Files_Written, "$SAVE_DIRECTORY\/$Filename");
  340.         $TOTAL_BYTES += $BytesRead;
  341.         $Confirmation{$File_Handle} = $BytesRead;
  342.  
  343.         close($File_Handle);
  344.         close(OUTFILE);
  345.  
  346.         chmod (0666, "$SAVE_DIRECTORY\/$Filename");
  347.     }
  348.  
  349.     $FILES_UPLOADED = scalar(keys(%Confirmation));
  350.  
  351.     
  352.     if ($TOTAL_BYTES > $MAXIMUM_UPLOAD && $MAXIMUM_UPLOAD > 0) {
  353.         foreach $File (@Files_Written) {
  354.             unlink $File;
  355.         }
  356.         
  357.         print header;
  358.         print <<__END_OF_HTML_CODE__;
  359.         
  360.         <HTML>
  361.         <HEAD>
  362.             <TITLE>Error: Limit Reached</TITLE>
  363.         </HEAD>
  364.         <BODY link="#CC0000" alink="#FF3300" vlink="#330099" text="#000000" bgcolor="#ffffff" background="../pics/weave.jpg">
  365.         <table border=0 width=590>  <!-- begin table-->
  366.         <tr>
  367.         <tr>
  368.         <td valign="top" width="110">
  369.         <img src="../pics/sidebar.gif" usemap="#sidebar.gif" border="0">
  370.         <br>
  371.         </td>
  372.         <td width="480">      <!-- total = 590 -->
  373.  
  374.         <H1>Limit Reached</H1>
  375.         <P>
  376.         You have reached your upload limit.  You attempted to upload <B>$FILES_UPLOADED</B> files, totalling 
  377.         <B>$TOTAL_BYTES</B>.  This exceeds the maximum limit of <B>$MAXIMUM_UPLOAD</B> bytes, set by the system 
  378.         administrator.  <B>None</B> of your files were successfully saved.  Please try again.
  379.         <P>
  380.         <HR SIZE=1>
  381. <center>
  382. <A HREF="http://skynet.csn.ul.ie/"> <IMG Border="0" SRC="../pics/skynet-button.gif"></a>
  383. </center>
  384.  
  385.         </td>
  386.         </tr>
  387.         </table>
  388.         </BODY>
  389.         </HTML>
  390.                 
  391. __END_OF_HTML_CODE__
  392.         exit;
  393.     }
  394.     
  395.     if ($SUCCESS_LOCATION !~ /^\s*$/) {
  396.         print $query->redirect($SUCCESS_LOCATION);
  397.     } else {
  398.     
  399.  
  400.         print header;
  401.         $command = join(' ',"mswordview","-o -","-s http:\/\/www.csn.ul.ie\/~caolan\/docs\/symbolfont","-d http:\/\/www.csn.ul.ie\/~caolan\/docs\/wingdingfont","-p http:\/\/www.csn.ul.ie\/~caolan\/docs\/patterns","-t 240","\"$SAVE_DIRECTORY\/$Filename\"");
  402.         system($command);
  403.  
  404.         foreach $key (keys (%Confirmation)) {
  405.             print "$key - $Confirmation{$key} bytes\n";
  406.         }
  407.         
  408.         print <<__END_OF_HTML_CODE__;
  409.         
  410.         <P>
  411.         <HR SIZE=1>
  412. <center>
  413. <A HREF="http://skynet.csn.ul.ie/"> <IMG Border="0" SRC="../pics/skynet-button.gif"></a>
  414. </center>
  415. __END_OF_HTML_CODE__
  416.         exit;    
  417.     }
  418.     
  419. # ---------------------------------------------------------------------
  420. # EOF
  421.